/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.validation.client;

import com.google.gwt.user.client.ui.HasValue;

/**
 * The {@link ValidationContext} builder.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ValidationContextBuilder {
  private ValidationContext context;
  private String[] bindingGroups;

  /**
   * Gets the context.
   * 
   * @return the context
   */
  ValidationContext getContext() {
    return context;
  }

  /**
   * Creates a validation context with the given input.
   * 
   * @param input the input.
   */
  ValidationContextBuilder(HasValue<?>[] inputs) {
    context = new ValidationContext(inputs);
  }

  /**
   * Sets the binding groups for the next {@link Validation}s to add.
   * 
   * @param groups the groups
   * @return this for continuously building
   */
  public ValidationContextBuilder forGroups(String... groups) {
    this.bindingGroups = groups;
    return this;
  }

  /**
   * Adds a {@link Validation} to the {@link ValidationContext}.
   * 
   * @param validation the validation
   * @return this for continuously building
   */
  public ValidationContextBuilder add(Validation validation) {
    if (validation.groups == null) {
      validation.groups = bindingGroups;
    }
    context.add(validation);
    return this;
  }

}
